package com.ge.energy.pgs.myfleet.database.query.base;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.ge.energy.pgs.myfleet.code.vo.ChatRoom;
import com.ge.energy.pgs.myfleet.code.vo.ChatRoomCategory;
import com.ge.energy.pgs.myfleet.code.vo.ChatRooms;
import com.ge.energy.pgs.myfleet.code.vo.Contact;
import com.ge.energy.pgs.myfleet.code.vo.ContactCategory;
import com.ge.energy.pgs.myfleet.code.vo.Contacts;
import com.ge.energy.pgs.myfleet.database.data.DataInitBean;
import com.ge.energy.pgs.myfleet.database.table.CategoryUserTable;
import com.ge.energy.pgs.myfleet.database.table.ContactCategoryTable;
import com.ge.energy.pgs.myfleet.database.table.ContactTable;
import com.ge.energy.pgs.myfleet.database.table.RoomCategoryTable;
import com.ge.energy.pgs.myfleet.database.table.RoomTable;
import com.ge.energy.pgs.myfleet.database.table.RoomsTable;
import com.ge.energy.pgs.myfleet.database.table.UserTable;
import com.google.gson.Gson;

public class Select {
	public static Contacts getContactList(String userId){
		Contacts backValue = new Contacts();
		
		Contact myUserInfo = new Contact();
		for(UserTable item : DataInitBean.userTable){
			if(item.getUserId().equals(userId)){
				myUserInfo.setContactaAdress(item.getUserAdress());
				myUserInfo.setContactDec(item.getUserDec());
				myUserInfo.setContactId(item.getUserId());
				myUserInfo.setContactImg(item.getUserImg());
				myUserInfo.setContactName(item.getUserName());
				myUserInfo.setContactSex(item.getUserSex());
				break;
			}
		}
		
		backValue.setMyUserInfo(myUserInfo);
		
		
		for(ContactTable item : DataInitBean.contactTable){
			
			if(item.getUserId().equals(userId)){
	
				ContactCategory cc = new ContactCategory();
				
				for(ContactCategoryTable item1 : DataInitBean.contactCategoryTable){
					
					
					if(item1.getId().equals(item.getCategoryId())){
						cc.setCategoryIndex(item1.getCategoryIndex());
						cc.setCategoryName(item1.getCategoryName());
						List<Contact> cts = new  ArrayList<Contact>();
						for(CategoryUserTable item2 : DataInitBean.categoryUserTable){
							if(item2.getCategoryId().equals(item1.getId())){
								for(UserTable item3 : DataInitBean.userTable){
									if(item2.getUserId().equals(item3.getUserId())){
										Contact ct = new Contact();
										ct.setContactaAdress(item3.getUserAdress());
										ct.setContactDec(item3.getUserDec());
										ct.setContactId(item3.getUserId());
										ct.setContactImg(item3.getUserImg());
										ct.setContactName(item3.getUserName());
										ct.setContactSex(item3.getUserSex());
										cts.add(ct);
									}
								}
							}
						}

						cc.setContactList(cts);
						
						if(cts.size() > 0){
							backValue.getCategoryList().add(cc);
						}
						
						break;
						
					}

				}

			}
		}
		
		
		return backValue;
	}
	
	public static ChatRooms getRoomList(){
		ChatRooms backValue = new ChatRooms();
		
		Map <String,List<String>> filter = new LinkedHashMap<String,List<String>>();
		
		for(RoomsTable item : DataInitBean.roomsTable){
			if( filter.containsKey(item.getCategoryId())){
				filter.get(item.getCategoryId()).add(item.getRoomId());
			}else{
				List<String> temp = new ArrayList<String>();
				temp.add(item.getRoomId());
				filter.put(item.getCategoryId(), temp);
			}
		}
		
		for(Entry<String, List<String>> entry :filter.entrySet()){
			ChatRoomCategory cate = new ChatRoomCategory();
			
			for(RoomCategoryTable item : DataInitBean.roomCategoryTable){
				if(item.getId().equals(entry.getKey())){
					cate.setCategoryName(item.getCategoryName());
					cate.setCategoryIndex(item.getCategoryIndex());
				}
			}
			
			for(String roomId : entry.getValue()){
				for(RoomTable item1 : DataInitBean.roomTable){
					if(roomId.equals(item1.getChatRoomId())){
						ChatRoom rm = new ChatRoom();
						rm.setChatRoomId(item1.getChatRoomId());
						rm.setChatRoomName(item1.getChatRoomName());
					//	rm.setChatRoomDec(item1.getChatRoomDec());
						rm.setChatRoomImg(item1.getChatRoomImg());
						cate.getChatRoomList().add(rm);
					}
				}
			}
			
			backValue.getChatRoomCategoryList().add(cate);
		}
		
		return backValue;
	}
	
	public static void main(String[] args) {
	//	DataInitBean.initData();
		Gson son = new Gson();
		String sons = son.toJson(Select.getRoomList());
		System.out.println(sons);
	}
}
